﻿/* Fixed Header Feature Start */
RegisterFixedHeader();
_spBodyOnLoadFunctionNames.push("initFixedHeaders");

function RegisterFixedHeader() {
    //var tOverrides = {};
    //tOverrides.OnPostRender = initFixedHeader;
    //p3ListDTClient.currentViews = [];
    //SPClientTemplates.TemplateManager.RegisterTemplateOverrides(tOverrides);

    p3ListDTClient.globalOverrideCtx.OnPostRender = initFixedHeader;
    p3ListDTClient.currentViews = [];
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(p3ListDTClient.globalOverrideCtx);
}

function updatefixedHeader() {
    var view;
    var width;
    var originalListHeaderTop;
    for (var viewId in p3ListDTClient.currentViews) {
        view = p3ListDTClient.currentViews[viewId];
        originalListHeaderTop = $(view.parentTd).offset().top + view.offsetToOriginalListHeader;
        if ((p3ListDTClient.workSpaceTop > originalListHeaderTop) && (p3ListDTClient.workSpaceTop < (originalListHeaderTop + view.height)) && (view.visible == false)) {
            if (view.fixedHeader == null)
                createFixedheader(view);
            view.visible = true;
            initFixedHeader(view.Ctx);
            $(view.fixedHeader).css("display", "block");

        } else {
            if (view.visible) {
                if ((p3ListDTClient.workSpaceTop < originalListHeaderTop) || (p3ListDTClient.workSpaceTop > (originalListHeaderTop + view.height))) {
                    $(view.fixedHeader).css("display", "none");
                    view.visible = false;
                } else {
                    $(view.fixedHeader).offset({ top: p3ListDTClient.workSpaceTop });
                }
            }
        }
    }
};

function createFixedheader(view) {

    view.Ctx.ListSchema.RenderSaveAsNewViewButton = false;
    view.Ctx.ListSchema.RenderViewSelectorPivotMenu = false;
    view.Ctx.ListSchema.InplaceSearchEnabled = false;
    view.Ctx.DisableHeroButton = true;
    var backgroundColor;
    try {
        backgroundColor = window.getComputedStyle(workSpace, null).getPropertyValue('background-color');
    } catch (e) {
        backgroundColor = "rgba(255,255,255,1)";
    }
    if (backgroundColor.startsWith('rgba')) {
        try {
            var rgba = backgroundColor.split(',');
            backgroundColor = rgba[0] + ',' + rgba[1] + ',' + rgba[2] + ',1)';
        } catch (e) { };
    }
    headerHTML = "<div id='fixedHeader-" + view.Ctx.wpq + "' style='position:absolute;display:none;background-color:" + backgroundColor + ";z-index:1;'>" + RenderHeaderTemplate(view.Ctx, view.RenderColNames) + "</table></div>";
    view.fixedHeader = $.parseHTML(headerHTML)[0];
    $(p3ListDTClient.workSpace).append(view.fixedHeader);
}


function initFixedHeader(ctx) {
    var originalListHeader;
    var headerHTML;
    var fixedHeaderElement;
    var view;
    p3ListDTClient.workSpace = document.getElementById("s4-workspace");
    var ctxClone = jQuery.extend(true, {}, ctx);
    p3ListDTClient.workSpaceTop = $(p3ListDTClient.workSpace).offset().top;
    view = p3ListDTClient.currentViews[ctx.view];
    if (view == null)
        view = { webPartId: "js-listviewthead-" + ctx.wpq, Ctx: ctxClone, visible: false, fixedHeader: null };
    originalListHeader = document.getElementById(view.webPartId).parentNode;
    view.parentTd = document.getElementById("script" + ctx.wpq);
    var children = view.parentTd.children;
    var i = 0; view.offsetToOriginalListHeader = 0;
    while (children[i].id != originalListHeader.id) {
        view.offsetToOriginalListHeader += $(children[i]).height();
        i++;
    }
    p3ListDTClient.currentViews[ctx.view] = view;
    p3ListDTClient.viewsExisting = true;
    fixWidthAndLeft(ctx.view);
}

function fixWidthAndLeft(viewId) {
    var view = p3ListDTClient.currentViews[viewId]
    if ((view.visible) && (view.fixedHeader != null)) {
        var fixedTHs = $(view.fixedHeader).find("th");
        var originalTHs = $(view.parentTd).find("th");
        var i = 0;
        for (var i = 0; i < originalTHs.length; i++) {
            $(fixedTHs[i]).html($(originalTHs[i]).html());
            $(fixedTHs[i]).attr("style", $(originalTHs[i]).attr("style"));
            width = $(originalTHs[i]).width();
            $(fixedTHs[i]).css("width", width);
            $(fixedTHs[i]).css("max-width", width);
            $(fixedTHs[i]).css("min-width", width);
            $(fixedTHs[i]).css("min-width", width);
        }
        $(view.fixedHeader).css("left", $(view.parentTd).offset().left);
        //$(view.fixedHeader).offset({ left: $(view.parentTd).offset().left });
    }
    //ms-core-menu-box ms-core-menu-hasIcons ms-core-defaultFont ms-shadow
    setTimeout(function () { view.height = $(view.parentTd).height() - view.offsetToOriginalListHeader; }, 500);
}

function initFixedHeaders() {
    if (p3ListDTClient.viewsExisting) {
        var workspaceObj = document.getElementById('s4-workspace');
        $(window).resize(function () {
            clearTimeout(resizeId);
            resizeId = setTimeout(function () {
                for (var originalHeaderId in p3ListDTClient.currentViews) {
                    if (p3ListDTClient.currentViews[originalHeaderId].visible)
                        fixWidthAndLeft(originalHeaderId);
                }
            }, 500);
        });
        $(workspaceObj).scroll(updatefixedHeader);
    }
}

/* Fixed Header Feature End */
